package com.drandcamp.fragmentlabl5;

import android.os.Bundle;
import android.app.Fragment;
import android.view.ViewGroup;
import android.view.View;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.content.Context;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Context c = getActivity().getApplicationContext();
        View vw = inflater.inflate(R.layout.fragment1, container, false);
        final String[] names={"Vanida", "Joshwin", "Dennis", "Rio", "Peggy"};
        final String[] cities={"Fresno", "Irvine", "Sanger", "Fresno", "Clovis"};
        ListView namesList = (ListView) vw.findViewById(R.id.names_list);
        ArrayAdapter<String> myArrayAdapt = new ArrayAdapter<String>(c,android.R.layout.simple_list_item_1, names);
        namesList.setAdapter(myArrayAdapt);
        namesList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // Find each view
                TextView selectedOption = (TextView) getActivity().findViewById(R.id.selectOption);
                ImageView imageViewPicture = (ImageView) getActivity().findViewById(R.id.imageViewPicture);
                // Set the TextView's text with the correct name
                selectedOption.setText("You selected: " + ((TextView) view).getText().toString()
                        + " from: " + cities[i] );
                // Get the ImageView's context and set the correct picture
                Context imgContext = imageViewPicture.getContext();
                int id = imgContext.getResources().getIdentifier(names[i].toLowerCase(), "drawable", imgContext.getPackageName());
                imageViewPicture.setImageResource(id);
            }
        });
        return vw;
    }



    
}
